package com.etop.ondoc.dao;

import static com.github.cuter44.nyafx.servlet.Params.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.hibernate.Criteria;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.etop.ondoc.beans.*;
import com.etop.ondoc.util.DefaultValue;
import com.etop.ondoc.util.HibernateUtil;
import com.etop.ondoc.util.Pager;
import com.github.cuter44.nyafx.dao.DaoBase;
public class BDocDao extends DaoBase<Document>{
	public BDocDao(){
		super();
	}
	  // SINGLETON
    private static class Singleton
    {
        public static BDocDao instance = new BDocDao();
    }

    public static BDocDao getInstance()
    {
        return(Singleton.instance);
    }
	@Override
	protected Class classOfT() {
		return Document.class;
	}
	public Document create(String name,User upUser,Category cate,String info){
		Document doc = new Document();
		cate = cate==null?DefaultValue.ROOT_CATEGORY:cate;
		doc.setCategory(cate);
		doc.setInfo(info);
		doc.setName(name);
		doc.setUpUser(upUser);
		doc.setUpDate(new Date());
		this.save(doc);
		this.close();
		return (doc);
		
	}
	public void add(Document docu){
		if(docu!=null)
		try {
			this.begin();
			this.save(docu);
			this.commit();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			this.close();
		}
	}
	 public Pager<Document> pager(int offset,int pagesize){
	    	
    	 Pager<Document> pages = null;
         try {
        	 pages = new Pager<>();
        	 DetachedCriteria dc = DetachedCriteria.forClass(Document.class);
        	 this.begin();
        	 
        	 List<Document> l = this.search(dc, offset, pagesize);
        	 long totalRecord =  (long) this.createQuery("select count(*) from Document").uniqueResult();
        	 pages.setDatas(l);
        	 pages.setPageOffset(offset);
        	 pages.setPageSize(pagesize);
        	 pages.setTotalRecord((int)totalRecord);
        	 
        	 this.commit();
         } catch (Exception e) {
             throw e;
         } finally {
             this.close();
         }
		return pages;
    	
    }
	 public Pager<Document> getSearchDoc(HttpServletRequest req,String hql,int offset,int pagesize){
		 
		 Pager<Document> pages = null;
			
		 try {
        	 pages = new Pager<>();
        	 this.begin();
        	 
        	 List<Document> l = this.createQuery(hql).setFirstResult(offset).setMaxResults(pagesize).list();
        	 hql=hql.substring(hql.indexOf("from"));
        	 long totalRecord =  (long) this.createQuery("select count(*) "+hql).uniqueResult();
        	 pages.setDatas(l);
        	 pages.setPageOffset(offset);
        	 pages.setPageSize(pagesize);
        	 pages.setTotalRecord((int)totalRecord);
        	 
        	 this.commit();
         } catch (Exception e) {
             throw e;
         } finally {
             this.close();
         }
		return pages;
	}
	 public String getHql(HttpServletRequest req){
		 	String NAME = "search";
			String DOC  = "doc";
			String PPT  = "ppt";
			String TXT  = "txt";
			String PDF  = "pdf";
			String XLS  = "xls";
			String TIME = "s1";
			
			String name = null;
			String doc = null;
			String txt = null;
			String pdf = null;
			String ppt = null;
			String xls = null;
	
			if(getString(req, NAME)==null){
				if(req.getSession().getAttribute(NAME)!=null){
					name = (String) req.getSession().getAttribute(NAME);
					req.getSession().setAttribute(NAME, name);
				}
			}
			else {
				name = getString(req, NAME);
				req.getSession().setAttribute(NAME, name);
			}
			
			if(getString(req, DOC)==null){
				if(req.getSession().getAttribute(DOC)!=null){
					doc = (String) req.getSession().getAttribute(DOC);
					req.getSession().setAttribute(DOC, doc);
				}
			}
			else {
				doc = getString(req, DOC);
				req.getSession().setAttribute(DOC, doc);
			}
			
			if(getString(req, PPT)==null){
				if(req.getSession().getAttribute(PPT)!=null){
					ppt = (String) req.getSession().getAttribute(PPT);
					req.getSession().setAttribute(PPT, ppt);
				}
			}
			else {
				ppt = getString(req, PPT);
				req.getSession().setAttribute(PPT, ppt);
			}
			
			if(getString(req, TXT)==null){
				if(req.getSession().getAttribute(TXT)!=null){
					txt = (String) req.getSession().getAttribute(TXT);
					req.getSession().setAttribute(TXT, txt);
				}
			}
			else {
				txt = getString(req, TXT);
				req.getSession().setAttribute(TXT, txt);
			}
			
			if(getString(req, PDF)==null){
				if(req.getSession().getAttribute(PDF)!=null){
					pdf = (String) req.getSession().getAttribute(PDF);
					req.getSession().setAttribute(PDF, pdf);
				}
			}
			else {
				pdf = getString(req, PDF);
				req.getSession().setAttribute(PDF, pdf);
			}
			
			if(getString(req, XLS)==null){
				if(req.getSession().getAttribute(XLS)!=null){
					xls = (String) req.getSession().getAttribute(XLS);
					req.getSession().setAttribute(XLS, xls);
				}
			}
			else {
				xls = getString(req, XLS);
				req.getSession().setAttribute(XLS, xls);
			}
			
			int    time = 0;
			if(req.getParameter(TIME)==null){
				if(req.getSession().getAttribute(TIME)!=null){
					time = (int)req.getSession().getAttribute(TIME);
					req.getSession().setAttribute(TIME, time);
				}
			}else {
				time = Integer.parseInt(req.getParameter(TIME));
				req.getSession().setAttribute(TIME,time);
			}
			
			Date d = new java.sql.Date(DefaultValue.now-time*DefaultValue.hm);
			
			String sql = "from Document d where (";
			int sqlLength = sql.length();
			if(doc!=null)sql+=" d.name like '%"+name+"%"+"doc' or ";
			if(ppt!=null)sql+=" d.name like '%"+name+"%"+"ppt' or ";
			if(txt!=null)sql+=" d.name like '%"+name+"%"+"txt' or ";
			if(pdf!=null)sql+=" d.name like '%"+name+"%"+"pdf' or ";
			if(xls!=null)sql+=" d.name like '%"+name+"%"+"xls' or ";
			if(sql.length()==sqlLength)sql+=" d.name like '%"+name+"%' or ";
			sql+="1=0";
			this.close();
			sql+=" ) and d.upDate >= '"+d+"'";
			return sql;
	 }
	 public List<Document> getHot(){
		 List<Document> l = null ;
		 BDocDao bdd = BDocDao.getInstance();
		 DetachedCriteria dc = DetachedCriteria.forClass(Document.class);
		 	dc.addOrder(Order.desc("downtotal"));
	 	try {
			bdd.begin();
			Criteria c = dc.getExecutableCriteria(
	            bdd.thisSession.get()
				);
			c.setFirstResult(0);
		 	c.setMaxResults(DefaultValue.hot);
			l = c.list();
			bdd.commit();
		} catch (Exception e) {
			e.printStackTrace();
		} finally{
		 	bdd.close();
		}
		return l;
	 }
}
